//
//  CCVirstorView.swift
//  swift_weibo
//
//  Created by ccl on 16/2/24.
//  Copyright © 2016年 LittleBoy. All rights reserved.
//

import UIKit
//
protocol VirstorViewDelegate : NSObjectProtocol{

    func VirstorViewloginBtnDidClick(virstor: UIView , btn: UIButton);
    
    func VirstorViewRegistBtnDidClick(virstor: UIView , btn: UIButton);

}



class CCVirstorView: UIView {
    
    weak var delegate: VirstorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame);
        
        
        backgroundColor = UIColor.whiteColor();
        setUI();
    
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func setupViewInfo(iconName : String, labText : String , isHome : Bool){
    
        if (isHome == true){
        
        }else{
          
            homeIconView.image = UIImage(named: iconName);
            iconView.hidden = true;
            messageLab.text = labText;
            sendSubviewToBack(maskIconView);
            
        }
    
    }
  // 登录
    func loginBtnDidClick(sender: UIButton){
        delegate?.VirstorViewloginBtnDidClick(self, btn: sender);
    
    }
  // 注册
    func registBtnDidClick(sender: UIButton){
    
        delegate?.VirstorViewRegistBtnDidClick(self , btn: sender);
    }
  
  // 旋转
    func strarRotation(){
     
        let baseAnima = CABasicAnimation(keyPath: "transform.rotation");
        baseAnima.toValue  = 2 * M_PI;

        baseAnima.duration = 20;
        baseAnima.repeatCount = MAXFLOAT;
        
        iconView.layer.addAnimation(baseAnima, forKey: nil );
        
    }
    private func setUI() {
       
        addSubview(iconView);
        addSubview(maskIconView);
        addSubview(homeIconView);
        addSubview(messageLab);
        addSubview(loginBtn);
        addSubview(regiestBtn);
      //  addSubview(maskIconView);
       

        // 取消自动布局
        iconView.translatesAutoresizingMaskIntoConstraints = false;
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        homeIconView.translatesAutoresizingMaskIntoConstraints = false;
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0));
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0));
        
        messageLab.translatesAutoresizingMaskIntoConstraints = false;
        addConstraint(NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: homeIconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0));
        addConstraint(NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20));
        
        // vfl 可视化语言
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(240)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["subView" : messageLab]));
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[sub(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["sub": messageLab]));
        
        regiestBtn.translatesAutoresizingMaskIntoConstraints = false;
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[sub(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["sub" : regiestBtn]));
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[sub(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["sub" : regiestBtn]));
        addConstraint(NSLayoutConstraint(item: regiestBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLab, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0));
        addConstraint(NSLayoutConstraint(item: regiestBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLab, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10));
        
        loginBtn.translatesAutoresizingMaskIntoConstraints = false;
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLab, attribute: NSLayoutAttribute.Right , multiplier: 1.0, constant: 0));
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: regiestBtn, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0));
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[sub(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["sub" : loginBtn]));
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[sub(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["sub" : loginBtn]));
        
        // 添加遮盖
        maskIconView.translatesAutoresizingMaskIntoConstraints = false;
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[sub]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["sub" : maskIconView]));
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(("V:|[sub]-170-|"), options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["sub" : maskIconView]));
          backgroundColor = UIColor(white: 237.0 / 255.0 , alpha: 1);
        
    }
    // MARK: - 懒加载控件

    // 大装盘
    lazy var iconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon");
        //imageV.image = UIImage(named: "visitordiscover_feed_image_smallicon");
        return UIImageView(image: image);
    }()
    // 罩子
    lazy var maskIconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon");
        
        return UIImageView(image: image);
    }()
    // 房子
    lazy var homeIconView: UIImageView = {
    
        let image = UIImage(named: "visitordiscover_feed_image_house");
        return UIImageView(image: image);
    }()
    // 文本
    lazy var messageLab: UILabel = {
    
        let labs = UILabel(frame: CGRectMake(0,0,240,40));
        labs.font = UIFont.systemFontOfSize(14);
        labs.textAlignment = NSTextAlignment.Center;
        labs.textColor = UIColor.darkGrayColor();
        labs.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过";
        // 文本显示宽度
        labs.preferredMaxLayoutWidth = 224.0;
        // 两行
        labs.numberOfLines = 2;
        
        return labs;
        
    }()
    // 登录
    lazy var loginBtn: UIButton = {
      
        let btn = UIButton(frame: CGRectMake(0, 0, 100, 40));
        let img = UIImage(named: "common_button_white_disable");
        let edg = UIEdgeInsetsMake((img?.size.height)! * 0.5, (img?.size.width)! * 0.5, (img?.size.height)! * 0.5, (img?.size.width)! * 0.5);
        let imgResize = img?.resizableImageWithCapInsets(edg, resizingMode: UIImageResizingMode.Stretch);
        btn.setBackgroundImage(imgResize, forState: UIControlState.Normal);
        btn.setTitle("登录", forState: UIControlState.Normal);
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal);
        btn.addTarget(self, action: "loginBtnDidClick:", forControlEvents: UIControlEvents.TouchUpInside);
        return btn
    }()
     // 注册
    lazy var regiestBtn: UIButton = {
        let btn = UIButton(frame: CGRectMake(0, 0, 100, 40));
        let img = UIImage(named:"common_button_white_disable");
        let edg = UIEdgeInsetsMake((img?.size.height)! * 0.5, (img?.size.width)! * 0.5, (img?.size.height)! * 0.5 , (img?.size.width)! * 0.5);
        let imgResize = img?.resizableImageWithCapInsets(edg, resizingMode: UIImageResizingMode.Stretch);
        btn.setBackgroundImage(imgResize, forState: UIControlState.Normal);
        btn.setTitle("注册", forState: UIControlState.Normal);
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal);
        btn.addTarget(self , action: "registBtnDidClick:", forControlEvents: UIControlEvents.TouchUpInside);
        return btn;
    }()

 }

